class TreeNode {
  constructor(val = 0, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function inorderTraversal(root) {
  const stack = [];
  const result = [];
  let current = root;

  while (current !== null || stack.length > 0) {
    // 沿着左子树不断压栈
    while (current !== null) {
      stack.push(current);
      current = current.left;
    }

    // 弹出栈顶节点并访问
    current = stack.pop();
    result.push(current.val);

    // 访问右子树
    current = current.right;
  }

  return result;
}

// 示例二叉树
//     1
//      \
//       2
//      /
//     3
const root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);

// 测试中序遍历
console.log(inorderTraversal(root)); // 输出 [1, 3, 2]
